/**
 * VUEshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.vueshop.com.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: soubao-java 2020-07-22 $
 */<template>
  <div>
    <div class="item-title">
      <div class="subject">
        <h3>商品分类管理</h3>
        <h5>网站文章分类添加与管理</h5>
      </div>
    </div>

    <div
      id="explanation"
      class="explanation"
      style="color: rgb(85, 85, 85); background-color: rgb(243, 244, 244); width: 98%; height: 100%;"
    >
      <div class="bckopa-tips">
        <div class="title">
          <img src="@/static/images/handd.png" alt />
          <h4 title="提示相关设置操作时应注意的要点">操作提示</h4>
        </div>
        <ul>
          <li>温馨提示：顶级分类（一级大类）设为推荐时才会在首页楼层中显示</li>
          <li>最多只能分类到三级</li>
          <li>佣金比例填写0-99整数</li>
        </ul>
      </div>
      <span title="收起提示" id="explanationZoom" style="display: block;"></span>
    </div>

    <div class="mDiv">
      <div class="ftitle">
        <h3>商品分类列表</h3>
      </div>
      <div class="fbutton">
        <div class="add" @click="toAdd()">
          <span>
            <i class="el-icon-circle-plus" /> 新增分类
          </span>
        </div>
      </div>
      <a href @click.prevent="refresh()" class="refresh-date">
        <div title="刷新数据" class="pReload">
          <i class="fa fa-refresh"></i>
        </div>
      </a>
    </div>

    <div class="tablebox">
      <el-table
        :data="tableData"
        ref="table"
        style="width: 100%;margin-bottom: 20px;"
        row-key="id"
        lazy
        :load="load"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column prop="id" label="分类ID" width="100"></el-table-column>
        <el-table-column label="分类名称" width="150">
          <template slot-scope="scope">
            <el-input @change="update(scope)" class="cat-name" v-model="scope.row.name"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="goods_type_name" label="模型" width="100">
          <template slot-scope="scope">{{scope.row.goods_type_name || '--'}}</template>
        </el-table-column>
        <el-table-column label="是否热卖" width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_hot"
              @change="update(scope)"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="是否显示" width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_show"
              @change="update(scope)"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="commission_rate" label="佣金比例" width="100"></el-table-column>
        <el-table-column label="分组" width="100">
          <template slot-scope="scope">
            <el-input @change="update(scope)" class="sort-input" v-model="scope.row.cat_group"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="排序" width="100">
          <template slot-scope="scope">
            <el-input @change="update(scope)" class="sort-input" v-model="scope.row.sort_order"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button-group>
              <el-button size="small" @click="toEdit(scope.row.id)" icon="el-icon-edit-outline">编辑</el-button>
              <el-button
                v-if="scope.row.level < 3"
                icon="el-icon-plus"
                @click="addChildren(scope.row.id)"
                size="small"
              >新增下级</el-button>
              <el-button size="small" @click="delFn(scope.row)" icon="el-icon-delete">删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import {
  getGoodsCategoryListAdmin,
  updateGoodsCategory,
  deleteGoodsCategory
} from "@/utils/api";
export default {
  data() {
    return {
      tableData: [],
      maps: new Map()
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      var that = this;
      getGoodsCategoryListAdmin(0).then(function(res) {
        res.forEach(e => {
          e.hasChildren = true;
        });
        that.tableData = res;
      });
    },
    refresh() {
      this.getList();
      this.$message.success("刷新成功！");
    },
    update(scope) {
      var that = this;
      updateGoodsCategory(scope.row).then(function(res) {
        if (res.status == 1) {
          that.$message.success("修改成功！");
          that.getList();
        } else {
          that.$message.error(res.msg);
        }
      });
    },
    delFn(row) {
      var that = this;
      this.$confirm("是否删除该分类吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteGoodsCategory(row.id).then(function(res) {
            if (res.status == 1) {
              that.$message.success("删除成功!");
              that.getList(); //重新查询一级数据
              //删除一级之下的数据时实现刷新table
              if (row.level > 1) {
                const { parent_id } = row;
                const { tree, treeNode, resolve } = that.maps.get(parent_id); //根据pid取出对应的节点数据
                that.$set(
                  that.$refs.table.store.states.lazyTreeNodeMap,
                  parent_id,
                  []
                ); //将对应节点下的数据清空，从而实现数据的重新加载
                that.load(tree, treeNode, resolve);
              }
            } else {
              that.$message.error(res.msg);
            }
          });
        })
        .catch(() => {
          that.$message.info("已取消删除");
        });
    },
    toAdd() {
      this.$router.push({
        name: "goods_category",
        params: {
          currentPage: this.currentPage
        }
      });
    },
    addChildren(id) {
      this.$router.push({
        name: "goods_category_add_child",
        params: {
          id: id,
          flag: 1
        }
      });
    },
    toEdit(id) {
      this.$router.push({
        name: "goods_category_edit",
        params: {
          id: id
        }
      });
    },
    load(tree, treeNode, resolve) {
      this.maps.set(tree.id, { tree, treeNode, resolve }); //将当前选中节点数据存储到maps中
      getGoodsCategoryListAdmin(tree.id).then(function(res) {
        if (tree.level < 2) {
          res.forEach(e => {
            e.hasChildren = true;
          });
        }
        resolve(res);
      });
    }
  }
};
</script>

<style scoped>
>>> .cat-name .el-input__inner {
  width: 100px;
}
>>> .sort-input .el-input__inner {
  width: 60px;
}
</style>
